package com.gm.app.web;

import java.util.List;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.gm.app.data.IndicationDAO;
import com.gm.app.model.Country;
import com.gm.app.model.Indication;
import com.gm.app.model.Resident;

@Controller
@RequestMapping("/indication")
public class IndicationController extends RegisterController {

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String get(@PathVariable("id") Long id, Model model) {
		Indication i = Indication.findIndication(id);
		model.addAttribute("indication", i == null ? new Indication() : i);
		return "indication/view";
	}

	@RequestMapping(method = RequestMethod.GET)
	public String list(
			Model model,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "address", required = false) String address,
			@RequestParam(value = "country", required = false) String country,
			@RequestParam(value = "comments", required = false) String comments,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "index", required = false) Integer index) {
		List<Indication> list = IndicationDAO.search(name, address, country,
				comments, page, index).getResultList();
		model.addAttribute("indication", list);
		return "indication/list";
	}

	@RequestMapping(method = RequestMethod.PUT)
	public String add(@Valid Indication i, Model model) {
		i.persist();
		return "redirect:/indication/" + i.getId();
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable("id") Long id, Model model) {
		Indication i = Indication.findIndication(id);
		if (i != null) {
			i.remove();
		}
		return "redirect:/indication";
	}

	@RequestMapping(method = RequestMethod.POST)
	@Transactional
	public String update(Indication i, BindingResult binding, Model model) {
		if (binding.hasErrors()) {
			model.addAttribute("indication", i);
			return "/indication/view";
		}
		i = (Indication) i.merge();
		return "redirect:/indication/" + i.getId();
	}

	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public String search(Model model,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "address", required = false) String address,
			@RequestParam(value = "country", required = false) String country,
			@RequestParam(value = "comments", required = false) String comments) {
		List<Indication> q = null;
		q = IndicationDAO.search(name, address, country, comments)
				.getResultList();
		model.addAttribute("indication", q);
		return "indication/list";
	}

}
